﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UBlockly.Scriptable;
using UBlockly.Compotent;

namespace UBlockly.Graphic
{
    [Serializable]
    internal class BlockColorData
    {
        public string Key = string.Empty;
        public Color Clr;

        public BlockColorData(string key, Color c)
        {
            Key = key;
            Clr = c;
        }
    }

    [Serializable]
    internal class BlockSpriteData
    {
        [DisplayOnly]
        public string Key = string.Empty;
        public Sprite Spt;

        public BlockSpriteData(string key, Sprite sp)
        {
            Key = key;
            Spt = sp;
        }
    }
    [Serializable]
    internal class BlockSizeData
    {
        [DisplayOnly]
        public string Key = string.Empty;
        public float Size;

        public BlockSizeData(string key, float sz)
        {
            Key = key;
            Size = sz;
        }
    }
    [Serializable]
    internal class BlockGetterData
    {
        [DisplayOnly]
        public string Key = string.Empty;
        public IParamGetterCompotent Getter;

        public BlockGetterData(string key, IParamGetterCompotent pgc)
        {
            this.Key = key;
            Getter = pgc;
        }
    }
    [Serializable]
    internal class BlockSetterData
    {
        [DisplayOnly]
        public string Key = string.Empty;
        public IParamSetterPanel Panel;

        public BlockSetterData(string key, IParamSetterPanel panel)
        {
            Key = key;
            Panel = panel;
        }
    }
    
    [CreateAssetMenu(fileName = "BlockGraphic", menuName = "Custom Assets/BlockGraphic Asset", order = 1)]
    public sealed class BlockGraphicAsset : ScriptableObject
    {
        #region data

        [SerializeField] private Font font;
        [SerializeField] private List<BlockSizeData> sizeDataList = new List<BlockSizeData>();
        [SerializeField] private List<BlockColorData> colorDataList = new List<BlockColorData>();
        [SerializeField] private List<BlockSpriteData> spriteDataList = new List<BlockSpriteData>();
        [SerializeField] private List<BlockGetterData> getterDataList = new List<BlockGetterData>();
        [SerializeField] private List<BlockSetterData> setterDataList = new List<BlockSetterData>();
        
        public BlockGraphicAsset()
        {
            var color = Enum.GetNames(typeof(ColorDefine));
            foreach (var cc in color)
            {
                colorDataList.Add(new BlockColorData(cc,Color.blue));
            }

            var sprite = Enum.GetNames(typeof(SpriteDefine));
            foreach (var sp in sprite)
            {
                spriteDataList.Add(new BlockSpriteData(sp,null));
            }

            var size = Enum.GetNames(typeof(SizeDefine));
            foreach (var sz in size)
            {
                sizeDataList.Add(new BlockSizeData(sz, 100));
            }

            var getter = Enum.GetNames(typeof(GetterDefine));
            foreach (var gt in getter)
            {
                getterDataList.Add(new BlockGetterData(gt, null));
            }

            var setter = Enum.GetNames(typeof(SetterDefine));
            foreach (var st in setter)
            {
                setterDataList.Add(new BlockSetterData(st, null));
            }
        }

        public Font GetFont()
        {
            return font;
        }

        public Color? GetColor(string key)
        {
            var result = colorDataList.Find(x => x.Key == key);
            if (result != null)
                return result.Clr;
            throw new SystemException($"GetColor {key} is null");
            return null;
        }

        public Sprite GetSprite(string key)
        {
            var result = spriteDataList.Find(x => x.Key == key);
            if (!result.Equals(null))
                return result.Spt;
            return null;
        }

        public float GetSize(string key)
        {
            var result = sizeDataList.Find(x => x.Key == key);
            if (!result.Equals(null))
                return result.Size;
            return 0;
        }

        public IParamGetterCompotent GetParamGetter(string key)
        {
            var result = getterDataList.Find(x => x.Key == key);
            if (result != null)
                return result.Getter;
            return null;
        }

        public IParamSetterPanel GetParamSetterPanel(string key)
        {
            var result = setterDataList.Find(x => x.Key == key);
            if (result != null)
                return result.Panel;
            return null;
        }

        #endregion
    }

}
